package com.mcc.generator.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotBlank;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.ArrayUtils;
import com.mcc.common.constant.GenConstants;
import com.mcc.common.utils.StringUtils;

/**
 * 业务表 gen_table
 *
 * @author gxy
 */
@ApiModel("生成表对象")
@Data
public class GenTable implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId
    @ApiModelProperty("ID")
    private Long tableId;

    @NotBlank(message = "表名称不能为空")
    @ApiModelProperty("表名称")
    private String tableName;

    @NotBlank(message = "表描述不能为空")
    @ApiModelProperty("表描述")
    private String tableComment;

    @NotBlank(message = "实体类名称不能为空")
    @ApiModelProperty("实体类名称(首字母大写)")
    private String className;

    @ApiModelProperty("使用的模板（crud单表操作 tree树表操作）")
    private String tplCategory;

    @NotBlank(message = "生成包路径不能为空")
    @ApiModelProperty("生成包路径")
    private String packageName;

    @NotBlank(message = "生成模块名不能为空")
    @ApiModelProperty("生成模块名")
    private String moduleName;

    @NotBlank(message = "生成业务名不能为空")
    @ApiModelProperty("生成业务名")
    private String businessName;

    @NotBlank(message = "生成功能名不能为空")
    @ApiModelProperty("生成功能名")
    private String functionName;

    @NotBlank(message = "作者不能为空")
    @ApiModelProperty("生成作者")
    private String functionAuthor;

    @ApiModelProperty("生成代码方式（0zip压缩包 1自定义路径）")
    private String genType;

    @ApiModelProperty("生成路径（不填默认项目路径）")
    private String genPath;

    @TableField(exist = false)
    @ApiModelProperty("主键信息")
    private GenTableColumn pkColumn;

    @TableField(exist = false)
    @ApiModelProperty("表列信息")
    private List<GenTableColumn> columns;

    @ApiModelProperty("其它生成选项")
    private String options;

    @TableField(exist = false)
    @ApiModelProperty("树编码字段")
    private String treeCode;

    @TableField(exist = false)
    @ApiModelProperty("树父编码字段")
    private String treeParentCode;

    @TableField(exist = false)
    @ApiModelProperty("树名称字段")
    private String treeName;

    @TableField(exist = false)
    @ApiModelProperty("上级菜单ID字段")
    private String parentMenuId;

    @TableField(exist = false)
    @ApiModelProperty("上级菜单名称字段")
    private String parentMenuName;

    @ApiModelProperty("创建者")
    private String createBy;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty("创建时间")
    private Date createTime;

    @ApiModelProperty("更新者")
    private String updateBy;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @ApiModelProperty("更新时间")
    private Date updateTime;

    @ApiModelProperty("备注")
    private String remark;

    @TableField(exist = false)
    @ApiModelProperty("开始时间")
    private String beginTime;

    @TableField(exist = false)
    @ApiModelProperty("结束时间")
    private String endTime;

    @TableField(exist = false)
    private Map<String, Object> params;

    public boolean isTree() {
        return isTree(this.tplCategory);
    }

    public static boolean isTree(String tplCategory) {
        return tplCategory != null && StringUtils.equals(GenConstants.TPL_TREE, tplCategory);
    }

    public boolean isCrud() {
        return isCrud(this.tplCategory);
    }

    public static boolean isCrud(String tplCategory) {
        return tplCategory != null && StringUtils.equals(GenConstants.TPL_CRUD, tplCategory);
    }

    public boolean isSuperColumn(String javaField) {
        return isSuperColumn(this.tplCategory, javaField);
    }

    public static boolean isSuperColumn(String tplCategory, String javaField) {
        if (isTree(tplCategory)) {
            return StringUtils.equalsAnyIgnoreCase(javaField,
                    ArrayUtils.addAll(GenConstants.TREE_ENTITY, GenConstants.BASE_ENTITY));
        }
        return StringUtils.equalsAnyIgnoreCase(javaField, GenConstants.BASE_ENTITY);
    }
}
